﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using System.Collections.Specialized;
using System.Windows.Controls;
using Microsoft.LightSwitch.Threading;
using System.ComponentModel;

namespace LightSwitchApplication
{
    public partial class Stakeholder
    {
        partial void trStakeholderCustomPropertyCollection_SelectionChanged()
        {
            chooseRightInputControl();
            
            trStakeholderCustomProperty selectedItem = this.trStakeholderCustomPropertyCollection.SelectedItem;
            
            // Notify if grid sth. changes in custom property grid
            // Solution Source:
            // http://social.msdn.microsoft.com/Forums/en-US/lightswitchgeneral/thread/6d782d26-9666-4ae1-b448-e0eb556d108f/
            if (selectedItem == null)
                return;

            Dispatchers.Main.BeginInvoke(() =>
            {
                ((INotifyPropertyChanged)selectedItem).PropertyChanged -= SelectdItemPropertyChanged;
                ((INotifyPropertyChanged)selectedItem).PropertyChanged += SelectdItemPropertyChanged;
            });
        }

        private void SelectdItemPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "tlCustomProperty":
                    deleteCustomPropertyValues();
                    chooseRightInputControl();
                    break;
                default:
                    break;
            }
        }

        private void deleteCustomPropertyValues()
        {
            if (this.trStakeholderCustomPropertyCollection.SelectedItem == null)
                return;

            this.trStakeholderCustomPropertyCollection.SelectedItem.CustomPropertyValue = null;

            if (this.trStakeholderCustomPropertyCollection.SelectedItem.tlCustomProperty == null)
                return;

            if (this.trStakeholderCustomPropertyCollection.SelectedItem.tlCustomProperty.IsChooseable)
            {
                if (this.trStakeholderCustomPropertyCollection.SelectedItem.tlCustomPropertyChoices == null)
                    return;
                if (this.trStakeholderCustomPropertyCollection.SelectedItem.tlCustomProperty ==
                    this.trStakeholderCustomPropertyCollection.SelectedItem.tlCustomPropertyChoices.tlCustomProperty)
                    return;

                this.trStakeholderCustomPropertyCollection.SelectedItem.tlCustomPropertyChoices = null;
            }
        }

        private void chooseRightInputControl()
        {
            setIsEnabledSavely("ValueChoice", false);
            setIsEnabledSavely("ValueInsert", false);
            
            if (this.trStakeholderCustomPropertyCollection.SelectedItem == null)
                return;

            if (this.trStakeholderCustomPropertyCollection.SelectedItem.tlCustomProperty == null)
                return;

            propCustomProperty = this.trStakeholderCustomPropertyCollection.SelectedItem.tlCustomProperty;

            if (this.trStakeholderCustomPropertyCollection.SelectedItem.tlCustomProperty.IsChooseable)
            {
                setIsEnabledSavely("ValueChoice", true);
                setIsEnabledSavely("ValueInsert", false);
                return;
            }

            setIsEnabledSavely("ValueChoice", false);
            setIsEnabledSavely("ValueInsert", true);
        }

        /// <summary>
        /// This method checks the state of IsEnabled before  changing it.
        /// This is done to prevent loops by changed properties.
        /// ToDo: Extend method to deal with any property and move it in helper class.
        /// </summary>
        /// <param name="strControl"></param>
        /// <param name="bIsVisible"></param>
        private void setIsEnabledSavely(string strControl, bool bIsVisible)
        {
            if (this.FindControl(strControl).IsVisible != bIsVisible)
                this.FindControl(strControl).IsVisible = bIsVisible;
        }
    }
}
